/*     */ package com.zimbra.cs.mailbox.acl;
/*     */ 
/*     */ import com.google.common.collect.Sets;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMailbox.Options;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.ACL;
/*     */ import com.zimbra.cs.mailbox.ACL.Grant;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.ScheduledTask;
/*     */ import com.zimbra.cs.mailbox.ScheduledTaskManager;
/*     */ import com.zimbra.cs.service.AuthProvider;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import com.zimbra.soap.mail.message.SendShareNotificationRequest;
/*     */ import com.zimbra.soap.mail.message.SendShareNotificationRequest.Action;
/*     */ import com.zimbra.soap.mail.type.EmailAddrInfo;
/*     */ import com.zimbra.soap.type.Id;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ExpireGrantsTask
/*     */   extends ScheduledTask
/*     */ {
/*     */   static final String TASK_NAME_PREFIX = "expireGrantsTask";
/*     */   static final String ITEM_ID_PROP_NAME = "itemId";
/*     */   
/*     */   public String getName()
/*     */   {
/*  51 */     return getTaskName(getProperty("itemId"));
/*     */   }
/*     */   
/*     */   private static String getTaskName(String itemIdStr) {
/*  55 */     return "expireGrantsTask" + itemIdStr;
/*     */   }
/*     */   
/*     */   private static String getTaskName(int itemId) {
/*  59 */     return getTaskName(Integer.toString(itemId));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public MailItem call()
/*     */     throws Exception
/*     */   {
/*  71 */     int itemId = Integer.valueOf(getProperty("itemId")).intValue();
/*  72 */     Mailbox mbox = MailboxManager.getInstance().getMailboxById(getMailboxId());
/*     */     MailItem item;
/*     */     try {
/*  75 */       item = mbox.getItemById(null, itemId, MailItem.Type.UNKNOWN);
/*     */     }
/*     */     catch (MailServiceException.NoSuchItemException e) {
/*  78 */       return null;
/*     */     }
/*  80 */     ACL acl = item.getACL();
/*  81 */     if (acl == null) {
/*  82 */       return null;
/*     */     }
/*  84 */     ZMailbox zMbox = getZMailbox(mbox);
/*  85 */     List<ACL.Grant> grants = acl.getGrants();
/*  86 */     long now = System.currentTimeMillis();
/*  87 */     boolean aGrantWithExpiryExists = false;
/*  88 */     for (ACL.Grant grant : grants) {
/*  89 */       long expiry = grant.getEffectiveExpiry(acl);
/*  90 */       if (expiry != 0L)
/*     */       {
/*     */ 
/*  93 */         aGrantWithExpiryExists = true;
/*  94 */         if (now > expiry) {
/*     */           String granteeId;
/*  96 */           switch (grant.getGranteeType()) {
/*     */           case 6: 
/*  98 */             granteeId = "99999999-9999-9999-9999-999999999999";
/*  99 */             break;
/*     */           case 3: 
/* 101 */             granteeId = "00000000-0000-0000-0000-000000000000";
/* 102 */             break;
/*     */           default: 
/* 104 */             granteeId = grant.getGranteeId();
/*     */           }
/*     */           try {
/* 107 */             String address = getGranteeAddress(grant);
/* 108 */             if (address != null) {
/* 109 */               sendGrantExpiryNotification(zMbox, itemId, address);
/*     */             }
/*     */           } finally {
/* 112 */             mbox.revokeAccess(null, true, itemId, granteeId);
/*     */           }
/*     */         }
/*     */       } }
/* 116 */     return aGrantWithExpiryExists ? item : null;
/*     */   }
/*     */   
/*     */   private static void sendGrantExpiryNotification(ZMailbox zMbox, int itemId, String address) throws ServiceException
/*     */   {
/* 121 */     SendShareNotificationRequest req = new SendShareNotificationRequest();
/* 122 */     req.setEmailAddresses(Sets.newHashSet(new EmailAddrInfo[] { new EmailAddrInfo(address) }));
/* 123 */     req.setAction(SendShareNotificationRequest.Action.expire);
/* 124 */     req.setItem(new Id(Integer.toString(itemId)));
/* 125 */     zMbox.invokeJaxb(req);
/*     */   }
/*     */   
/*     */   private static String getGranteeAddress(ACL.Grant grant) throws ServiceException {
/* 129 */     switch (grant.getGranteeType()) {
/*     */     case 1: 
/* 131 */       Account granteeAcct = Provisioning.getInstance().get(Key.AccountBy.id, grant.getGranteeId());
/* 132 */       if (granteeAcct != null) {
/* 133 */         return granteeAcct.getName();
/*     */       }
/*     */       break;
/*     */     case 7: 
/* 137 */       return grant.getGranteeId();
/*     */     default: 
/* 139 */       return null;
/*     */     }
/* 141 */     return null;
/*     */   }
/*     */   
/*     */   private static ZMailbox getZMailbox(Mailbox mbox) throws ServiceException {
/* 145 */     Account account = mbox.getAccount();
/* 146 */     ZMailbox.Options options = new ZMailbox.Options();
/* 147 */     options.setNoSession(true);
/* 148 */     options.setAuthToken(AuthProvider.getAuthToken(account).toZAuthToken());
/* 149 */     options.setUri(AccountUtil.getSoapUri(account));
/* 150 */     return new ZMailbox(options);
/*     */   }
/*     */   
/*     */   static void cancel(int mailboxId, int itemId) throws ServiceException {
/* 154 */     ScheduledTaskManager.cancel(ExpireGrantsTask.class.getName(), getTaskName(itemId), mailboxId, false);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/acl/ExpireGrantsTask.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */